From a9aa3decf6b36a968cb66fc6dc48b1756d1f7a8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20L=C3=BCnborg?= Date: Wed, 4 Jul 2018 13:12:18 +0200 Subject: [PATCH] fix: underscore escaping for dotfiles (#1737) * WIP: fix undersocre escaping for dotfiles (fix #1732) * Fix charAt * adding a short explanation to the docs. * improved wording --- docs/dev-guide/plugin-dev.md | 24 ++++++++++++++++++++++++ packages/@vue/cli/lib/GeneratorAPI.js | 5 ++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/dev-guide/plugin-dev.md b/docs/dev-guide/plugin-dev.md index 701d0714e..593ddbb77 100644 --- a/docs/dev-guide/plugin-dev.md +++ b/docs/dev-guide/plugin-dev.md @@ -235,6 +235,30 @@ export default { <%# END_REPLACE %> ``` +#### Filename edge cases + +If you want to render a template file that either begins with a dot (i.e. `.env`) you will have to follow a specific naming convention, since dotfiles are ignored when publishing your plugin to npm: +``` +# dotfile templates have to use an underscore instead of the dot: + +/generator/template/_env + +# When calling api.render('./template'), this will be rendered in the project folder as: + +.env +``` +Consequently, this means that you also have to follow a special naming convention if you want to render file whose name actually begins with an underscore: +``` +# such templates have to use two underscores instead of the dot: + +/generator/template/__variables.scss + +# When calling api.render('./template'), this will be rendered in the project folder as: + +_variables.scss +``` + + ### Prompts #### Prompts for Built-in Plugins diff --git a/packages/@vue/cli/lib/GeneratorAPI.js b/packages/@vue/cli/lib/GeneratorAPI.js index f5925520c..afa1c836c 100644 --- a/packages/@vue/cli/lib/GeneratorAPI.js +++ b/packages/@vue/cli/lib/GeneratorAPI.js @@ -138,9 +138,12 @@ class GeneratorAPI { let filename = path.basename(rawPath) // dotfiles are ignored when published to npm, therefore in templates // we need to use underscore instead (e.g. "_gitignore") - if (filename.charAt(0) === '_') { + if (filename.charAt(0) === '_' && filename.charAt(1) !== '_') { filename = `.${filename.slice(1)}` } + if (filename.charAt(0) === '_' && filename.charAt(1) === '_') { + filename = `${filename.slice(1)}` + } const targetPath = path.join(path.dirname(rawPath), filename) const sourcePath = path.resolve(source, rawPath) const content = renderFile(sourcePath, data, ejsOptions)