简体   繁体   English

如何从Vue单个文件中提取JS和CSS到*.js和*.css

[英]How to extract JS and CSS from Vue single file to *.js and *.css

Suppose I have wrote a omponent a.vue ,After packaging of webpack,some HTML that has used a.vue looks like this:假设我写了一个a.vue ,在 webpack 打包后,一些使用过 a.vue 的a.vue看起来是这样的:

......
<script src="page.js"></script>
<link href="page.css" rel="stylesheet"/>
......

JS and CSS in a.Vue are packed into page.js and page.css .I want to know how to extract JS and CSS into separate files.Then HTML looks like: JS and CSS in a.Vue are packed into page.js and page.css .I want to know how to extract JS and CSS into separate files.Then HTML looks like:

<script src="a.js"></script>
<script src="page.js"></script>
<link href="a.css" rel="stylesheet"/>
<link href="page.css" rel="stylesheet"/>

I know it's not good to do this, I just want to know how to do it.我知道这样做不好,我只是想知道该怎么做。

I found a simmilar question on github.我在 github 上发现了一个类似的问题。 Maybe you can try the given solution也许您可以尝试给定的解决方案

https://github.com/vuejs/vue-loader/issues/681 https://github.com/vuejs/vue-loader/issues/681

configure webpack.prod.conf.js
config.entry = {}
config.entry[one.name] = one.path
config.output.libraryTarget = 'umd'
config.output.library = `${namespace}${one.importName}`
config.output.filename = `index.min.js`
config.output.path = path.resolve(__dirname, `../dist/components/${one.name}/`)

built it with

webpack(config, function (err, stats) {})

Personally I LOVE Single File Components (SFC) but it's really easy to separate html, css and js with Vue.我个人喜欢单文件组件 (SFC),但使用 Vue 分离 html、css 和 js 真的很容易。

MyComponent.vue我的组件.vue

<template>
  // html
</template>

<script src="./MyComponent.js"></script>
<style scoped src="./MyComponent.css">
</style>

See a simple example on Codesandbox 查看 Codesandbox 上的一个简单示例

Use vue-cli create project then it can auto extract css and js.使用vue-cli创建项目然后它可以自动提取 css 和 js。

I'm checking its webpack configuration我正在检查它的 webpack 配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM