Skip to content

Commit

Permalink
Merge pull request #49 from zackljackson/nested-chunknames
Browse files Browse the repository at this point in the history
feat(loadOption): Nested chunknames as major release
  • Loading branch information
faceyspacey authored May 8, 2018
2 parents 0f77ffd + c1bb186 commit 161713a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage
node_modules
*.log
.idea
.DS_Store
58 changes: 58 additions & 0 deletions __tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,64 @@ _universalImport({
"
`;
exports[`static import (import as function with relative paths + nested folder) 1`] = `
"
const obj = {component:()=>import(\`../components/nestedComponent\`)}; ()=> obj.component()
↓ ↓ ↓ ↓ ↓ ↓
var _path = _interopRequireDefault(require(\\"path\\")).default;
var _importCss = _interopRequireDefault(require(\\"babel-plugin-universal-import/importCss\\")).default;
var _universalImport = _interopRequireDefault(require(\\"babel-plugin-universal-import/universalImport\\")).default;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const obj = {
component: () => _universalImport({
id: \\"../components/nestedComponent\\",
file: \\"currentFile.js\\",
load: () => Promise.all([import(
/* webpackChunkName: 'components-nestedComponent' */
\`../components/nestedComponent\`), _importCss(\\"components/nestedComponent\\", {})]).then(proms => proms[0]),
path: () => _path.join(__dirname, \`../components/nestedComponent\`),
resolve: () => require.resolveWeak(\`../components/nestedComponent\`),
chunkName: () => \\"components-nestedComponent\\"
})
};
() => obj.component();
"
`;
exports[`static import (relative paths + nested folder) 1`] = `
"
import(\`../components/nestedComponent\`)
↓ ↓ ↓ ↓ ↓ ↓
var _path = _interopRequireDefault(require(\\"path\\")).default;
var _importCss = _interopRequireDefault(require(\\"babel-plugin-universal-import/importCss\\")).default;
var _universalImport = _interopRequireDefault(require(\\"babel-plugin-universal-import/universalImport\\")).default;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
_universalImport({
id: \\"../components/nestedComponent\\",
file: \\"currentFile.js\\",
load: () => Promise.all([import(
/* webpackChunkName: 'components-nestedComponent' */
\`../components/nestedComponent\`), _importCss(\\"components/nestedComponent\\", {})]).then(proms => proms[0]),
path: () => _path.join(__dirname, \`../components/nestedComponent\`),
resolve: () => require.resolveWeak(\`../components/nestedComponent\`),
chunkName: () => \\"components-nestedComponent\\"
});
"
`;
exports[`static import (string template + relative paths) 1`] = `
"
import(\`../../base\`)
Expand Down
4 changes: 4 additions & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pluginTester({
'static import (with file extension)': 'import("./Foo.js")',
'static import (string template)': 'import(`./base`)',
'static import (string template + relative paths)': 'import(`../../base`)',
'static import (import as function with relative paths + nested folder)':
'const obj = {component:()=>import(`../components/nestedComponent`)}; ()=> obj.component()',
'static import (relative paths + nested folder)':
'import(`../components/nestedComponent`)',
'dynamic import (string template)': 'import(`./base/${page}`)',
'dynamic import (string template with nested folder)':
'import(`./base/${page}/nested/folder`)',
Expand Down
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ function chunkNameOption(t, chunkNameTemplate, importArgNode) {
return t.objectProperty(t.identifier('chunkName'), chunkName)
}

function checkForNestedChunkName(node) {
const generatedChunkName = getMagicCommentChunkName(node)
const isNested =
generatedChunkName.indexOf('[request]') === -1 &&
generatedChunkName.indexOf('/') > -1
return isNested && prepareChunkNamePath(generatedChunkName)
}

module.exports = function universalImportPlugin({ types: t, template }) {
const chunkNameTemplate = template('() => MODULE')
const pathTemplate = template('() => PATH.join(__dirname, MODULE)')
Expand All @@ -214,6 +222,10 @@ module.exports = function universalImportPlugin({ types: t, template }) {

const importArgNode = getImportArgPath(p).node
t.existingChunkName = existingMagicCommentChunkName(importArgNode)
// no existing chunkname, no problem - we will reuse that for fixing nested chunk names
if (!t.existingChunkName) {
t.existingChunkName = checkForNestedChunkName(importArgNode)
}
const universalImport = getImport(p, IMPORT_UNIVERSAL_DEFAULT)

const cssOptions = {
Expand Down

0 comments on commit 161713a

Please sign in to comment.