Skip to content

Commit

Permalink
fix: fix resolving deps with css entry (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 1, 2020
1 parent ad14ef4 commit 6e06fcf
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"lint-staged": "^10.1.6",
"lodash-es": "^4.17.15",
"moment": "^2.26.0",
"normalize.css": "^8.0.1",
"npm-run-all": "^4.1.5",
"postcss-nesting": "^7.0.1",
"preact": "^10.4.1",
Expand Down
7 changes: 6 additions & 1 deletion playground/TestModuleResolve.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<div class="browser-field-resolve" :class="browserFieldResolve">
resolve browser field in package.json: {{ browserFieldResolve }}
</div>
<div class="css-entry-resolve" :class="cssEntry">
resolve dep w/ css entry point: {{ cssEntry }}
</div>
</template>

<script>
Expand All @@ -25,6 +28,7 @@ import { add } from 'lodash-es'
import { foo } from './util'
import { bar } from './util/bar.util'
import value from 'resolve-browser-field-test-package'
import css from 'normalize.css'
export default {
setup() {
Expand All @@ -34,7 +38,8 @@ export default {
optResolve: typeof add === 'function' ? 'ok' : 'error',
indexResolve: foo() ? 'ok' : 'error',
dotResolve: bar() ? 'ok' : 'error',
browserFieldResolve: value === 'success' ? 'ok' : 'error'
browserFieldResolve: value === 'success' ? 'ok' : 'error',
cssEntry: typeof css === 'string' ? 'ok' : 'error'
}
}
}
Expand Down
1 change: 1 addition & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"bootstrap": "link:../node_modules/bootstrap",
"lodash-es": "link:../node_modules/lodash-es",
"moment": "link:../node_modules/moment",
"normalize.css": "link:../node_modules/normalize.css",
"resolve-browser-field-test-package": "link:./resolve-browser-field",
"rewrite-optimized-test-package": "link:./rewrite-optimized/test-package"
}
Expand Down
4 changes: 4 additions & 0 deletions playground/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
version "0.0.0"
uid ""

"normalize.css@link:../node_modules/normalize.css":
version "0.0.0"
uid ""

"resolve-browser-field-test-package@link:./resolve-browser-field":
version "0.0.0"
uid ""
Expand Down
14 changes: 7 additions & 7 deletions src/node/build/buildPluginCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ export const createBuildCssPlugin = (
return {
code: modules
? `export default ${JSON.stringify(modules)}`
: cssCodeSplit
? // If code-splitting CSS, inject a fake marker to avoid the module
// from being tree-shaken. This preserves the .css file as a
// module in the chunk's metadata so that we can retrive them in
// renderChunk.
`${cssInjectionMarker}()\n`
: ``,
: (cssCodeSplit
? // If code-splitting CSS, inject a fake marker to avoid the module
// from being tree-shaken. This preserves the .css file as a
// module in the chunk's metadata so that we can retrive them in
// renderChunk.
`${cssInjectionMarker}()\n`
: ``) + `export default ${JSON.stringify(css)}`,
map: null
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/node/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export function resolveBareModuleRequest(
return id
}

let isEntry = false
const basedir = path.dirname(resolver.requestToFile(importer))
const pkgInfo = resolveNodeModule(basedir, id)
if (pkgInfo) {
Expand All @@ -244,14 +245,16 @@ export function resolveBareModuleRequest(
`package.json.`
)
)
} else {
isEntry = true
id = pkgInfo.entry
}
return pkgInfo.entry || id
}

// check and warn deep imports on optimized modules
const ext = path.extname(id)
if (!ext || jsSrcRE.test(ext)) {
const deepMatch = id.match(deepImportRE)
const deepMatch = !isEntry && id.match(deepImportRE)
if (deepMatch) {
const depId = deepMatch[1] || deepMatch[2]
if (resolveOptimizedModule(root, depId)) {
Expand Down
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ describe('vite', () => {
expect(await getText('.index-resolve')).toMatch('ok')
expect(await getText('.dot-resolve')).toMatch('ok')
expect(await getText('.browser-field-resolve')).toMatch('ok')
expect(await getText('.css-entry-resolve')).toMatch('ok')
})

if (!isBuild) {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4641,6 +4641,11 @@ normalize-path@^3.0.0, normalize-path@~3.0.0:
resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"
integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==

normalize.css@^8.0.1:
version "8.0.1"
resolved "https://registry.yarnpkg.com/normalize.css/-/normalize.css-8.0.1.tgz#9b98a208738b9cc2634caacbc42d131c97487bf3"
integrity sha512-qizSNPO93t1YUuUhP22btGOo3chcvDFqFaj2TRybP0DMxkHOCTYwp3n34fel4a31ORXy4m1Xq0Gyqpb5m33qIg==

npm-run-all@^4.1.5:
version "4.1.5"
resolved "https://registry.yarnpkg.com/npm-run-all/-/npm-run-all-4.1.5.tgz#04476202a15ee0e2e214080861bff12a51d98fba"
Expand Down

0 comments on commit 6e06fcf

Please sign in to comment.